package the.formula.calculator;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.TableLayout;
import android.widget.TextView;

public class SpecificHeat extends Activity{

	private EditText enthalpy;
	private EditText specificHeatCapacity;
	private EditText initialTemperature;
	private EditText finalTemperature;
	private EditText mass;
	private boolean answerAdded;
	private double answer = 0;
	private LinearLayout ll;
	private RadioGroup rg;
	private TextView tv;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.specific_heat_activity);
        enthalpy = (EditText) findViewById(R.id.enthalpy);
        specificHeatCapacity = (EditText) findViewById(R.id.specificHeat);
        initialTemperature = (EditText) findViewById(R.id.initialTemp);
        finalTemperature = (EditText) findViewById(R.id.finalTemp);
        mass = (EditText) findViewById(R.id.mass);
        ll = (LinearLayout) findViewById(R.id.SHlayout);
        tv = new TextView(this);
        rg = (RadioGroup) findViewById(R.id.specificHeatRadio);
    }
        
    public void onClick(View v) {
        switch(v.getId()) {
            case R.id.specificHeatSolve:
            	solveSpecificHeat();
                break;
        }
    }
    
	private void solveSpecificHeat() {
		String sEnthalpy = enthalpy.getText().toString();
		String sSpecificHeat = specificHeatCapacity.getText().toString();
		String sInitialTemp = initialTemperature.getText().toString();
		String sFinalTemp = finalTemperature.getText().toString();
		String sMass = mass.getText().toString();
		double enthalpyNum = 0;
		double specificHeatNum = 0;
		double initialTempNum = 0;
		double finalTempNum = 0;
		double tempChangeNum = 0;
		double massNum = 0;
		
		if(answerAdded) {
			ll.removeView(tv);
		}

		switch(rg.getCheckedRadioButtonId()) {
			case R.id.shRadio:
				enthalpyNum = Double.parseDouble(sEnthalpy);
				initialTempNum = Double.parseDouble(sInitialTemp);
				finalTempNum = Double.parseDouble(sFinalTemp);
				massNum = Double.parseDouble(sMass);
				answer = specificHeatC(enthalpyNum, massNum, (finalTempNum - initialTempNum));
				tv.setText("Specific Heat: " + answer);
				break;
			case R.id.massRadio:
				enthalpyNum = Double.parseDouble(sEnthalpy);
				initialTempNum = Double.parseDouble(sInitialTemp);
				finalTempNum = Double.parseDouble(sFinalTemp);
				specificHeatNum = Double.parseDouble(sSpecificHeat);
				answer = specificHeatM(enthalpyNum, specificHeatNum, (finalTempNum - initialTempNum));
				tv.setText("Mass: " + answer);
				break;
			case R.id.initialRadio:
				enthalpyNum = Double.parseDouble(sEnthalpy);
				finalTempNum = Double.parseDouble(sFinalTemp);
				massNum = Double.parseDouble(sMass);
				specificHeatNum = Double.parseDouble(sSpecificHeat);
				answer = specificHeatTi(enthalpyNum, specificHeatNum, massNum, finalTempNum);
				tv.setText("Initial temperature: " + answer);
				break;
			case R.id.finalRadio:
				enthalpyNum = Double.parseDouble(sEnthalpy);
				initialTempNum = Double.parseDouble(sInitialTemp);
				massNum = Double.parseDouble(sMass);
				specificHeatNum = Double.parseDouble(sSpecificHeat);
				answer = specificHeatTf(enthalpyNum, specificHeatNum, massNum, initialTempNum);
				tv.setText("Final temperature: " + answer);
				break;
			case R.id.enthalpyRadio:
				initialTempNum = Double.parseDouble(sInitialTemp);
				finalTempNum = Double.parseDouble(sFinalTemp);
				massNum = Double.parseDouble(sMass);
				specificHeatNum = Double.parseDouble(sSpecificHeat);
				answer = specificHeat(specificHeatNum, massNum, (finalTempNum - initialTempNum));
				tv.setText("Enthalpy: " + answer);
				break;
		}
		ll.addView(tv);
		answerAdded = true;
	}

		/****                   Specific heat capacity                  ****/
	    /***            				Q = cmt             		    ***/
	    
	private double specificHeat(double specificHeatCapacity, double mass, double temperature) {
	    double result = specificHeatCapacity * mass * temperature;
	    return result;
	}
	
	private double specificHeatC(double specificHeat, double mass, double temperature) {
	    double result = specificHeat / (mass * temperature);
	    return result;
	}
	
	private double specificHeatM(double specificHeat, double specificHeatCapacity, double temperature) {
	    double result = specificHeat / (specificHeatCapacity * temperature);
	    return result;
	}
	
	private double specificHeatTf(double specificHeat, double specificHeatCapacity, double mass, double initialTemperature) {
	    double result = (specificHeat / (specificHeatCapacity * mass)) + initialTemperature;
	    return result;
	}
	
	private double specificHeatTi(double specificHeat, double specificHeatCapacity, double mass, double finalTemperature) {
	    double result = finalTemperature - (specificHeat / (specificHeatCapacity * mass));
	    return result;
	}
}
